package com.xtaifghy.wugui;

import com.xtaifghy.wugui.util.Util;

import java.util.ArrayList;
import java.util.Random;

public class GameState5 implements GameStateInterface{
    private ArrayList<ArrayList<boolean[]>> dict;
    private int state = 0xff;
    private int goalState = 0x0f;
    Random rand = new Random();
    private static int[] actions={
            Integer.valueOf("1011",2),//{0,1,3},//1
            Integer.valueOf("111",2),//{0,1,2},//2
            Integer.valueOf("10110",2),//{1,2,4},//3
            Integer.valueOf("101001",2),//{0,5,3},//8
            Integer.valueOf("1011010",2),//{1,4,6,3},//0
            Integer.valueOf("10010100",2),//{2,4,7},//4
            Integer.valueOf("1101000",2),//{6,5,3},//7
            Integer.valueOf("11100000",2),//{6,5,7},//6
            Integer.valueOf("11010000",2),//{6,4,7},//5
    };

    public GameState5() {
        randomGoalState();
        initDict();
    }

    private void initDict(){
        dict = new ArrayList<ArrayList<boolean[]>>();
        GameState4 gs4 = new GameState4();
        gs4.setState(Util.intToBArr(0xff,8));
        for (int i = 0; i < 256; i++) {
            gs4.setGoalState(Util.intToBArr(i,8));
            dict.add(gs4.getPaths());
        }
    }

    @Override
    public ArrayList<boolean[]> getPaths() {
        return dict.get(0x000000ff^(goalState^state));
    }

    @Override
    public void randomGoalState() {
        goalState = rand.nextInt(256);
    }

    @Override
    public void setState(boolean[] state) {
        this.state = Util.bArrToInt(state);
    }

    @Override
    public boolean[] getState() {
        return Util.intToBArr(state,8);
    }

    @Override
    public void setGoalState(boolean[] goalState) {
        this.goalState = Util.bArrToInt(goalState);
    }

    @Override
    public boolean[] getGoalState() {
        return Util.intToBArr(goalState,8);
    }

    @Override
    public void doAction(int a) {
        state = state^actions[a];
    }

    @Override
    public boolean testState() {
        return(state==goalState);
    }
}